// pages/mine-edit/mine-edit.js
const re = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoEmail: '',
    info: {
      id: "",   // 学号
      name: "",
      avatar: "",
      avatarUrl: "",
      email: "",
      classId: 1,
      className: "",
      semester: 1,
      token: null
    }
  },
  editAvatar() {
    const thisPtr = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success(res) {
        // console.log(res.tempFiles[0])
        re.uploadImg(res.tempFiles[0]).then(res => {
          const info = thisPtr.data.info
          const avatar = res.data.fileName
          info['avatar'] = avatar
          info['avatarUrl'] = re.serverUrl + '/img/' + avatar
          thisPtr.setData({
            info: info
          })
        })
      }
    })
  },
  btnUpdate() {
    // 提交
    const data = {
      id: this.data.info.id,
      avatar: this.data.info.avatar,
      email: this.data.infoEmail
    }
    re.postRequest('/student/update', data).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    re.getRequest(`/student/info/${options.stuId}`).then(res => {
      const data = res.data
      if (res.data) {
        data['avatarUrl'] = re.serverUrl + '/img/' + data.avatar
        this.setData({
          info: data,
          infoEmail: data.email
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})